const path = require('path')
const fs = require('fs').promises;
const mime = require('mime')


const static = (dirname) => {
    console.log('static')
    return async (ctx, next) => {
        const filePath = path.join(dirname, ctx.path);
        try {
            const statObj = await fs.stat(filePath);
            if (statObj.isDirectory()) {
                filePath = path.join(filePath, 'index.html')
            }
            ctx.set('Content-Type', mime.getType(filePath) + ';charset=utf-8')
            ctx.body = await fs.readFile(filePath)
            console.log(ctx.body)
        } catch (error) {
            console.log(error)
            await next();
        }
    }
}

module.exports = static